﻿using System;
using System.Diagnostics;


namespace GPSTrackingLogCustom
{
    public class ListenerLog
    {
        enum Category
        {
            Information = 0,
            Warning = 1,
            Error  = 2
        }

       

        private static string GetCallingMethod(StackFrame frame)
        {            
            var method = frame.GetMethod();
            return method.DeclaringType.FullName + "." + method.Name;
        }
        
        /// <summary>
        /// Logs an informational message
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Info(string message, string gpsModel)
        {
            GPSTrackingServerDB.Entities.ListenerLog LogEntity = new GPSTrackingServerDB.Entities.ListenerLog();
            LogEntity.DateTime = DateTime.Now;
            LogEntity.Category = Category.Information.ToString();
            LogEntity.Method = GetCallingMethod(new StackFrame(1));            
            LogEntity.Message = message;
            LogEntity.GpsModel = gpsModel;

            GPSTrackingServerDB.Data.DataRepository.ListenerLogProvider.Insert(LogEntity);

        }

        /// <summary>
        /// Logs a warning message
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Warning(string message, string gpsModel)
        {
            GPSTrackingServerDB.Entities.ListenerLog LogEntity = new GPSTrackingServerDB.Entities.ListenerLog();
            LogEntity.DateTime = DateTime.Now;
            LogEntity.Category = Category.Warning.ToString();
            LogEntity.Method = GetCallingMethod(new StackFrame(1));
            LogEntity.Message = message;
            LogEntity.GpsModel = gpsModel;

            GPSTrackingServerDB.Data.DataRepository.ListenerLogProvider.Insert(LogEntity);         
        }

        /// <summary>
        /// Logs an error message
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Error(string message, string gpsModel)
        {
            GPSTrackingServerDB.Entities.ListenerLog LogEntity = new GPSTrackingServerDB.Entities.ListenerLog();
            LogEntity.DateTime = DateTime.Now;
            LogEntity.Category = Category.Error.ToString();
            LogEntity.Method = GetCallingMethod(new StackFrame(1));
            LogEntity.Message = message;
            LogEntity.GpsModel = gpsModel;

            GPSTrackingServerDB.Data.DataRepository.ListenerLogProvider.Insert(LogEntity); 
        }

        


    }
}
